<?php
class UsersController extends AppController
{
	
	var $name = 'Users';
	var $helpers = array('Html', 'Form', 'Javascript');
	var $layout = 'admin';
	//Mã Hóa Password 'Auth'
	var $components = array('Cookie', 'RequestHandler', 'Auth');
	
	function beforeFilter()
	{
		parent::beforeFilter();
		$this->Auth->allow('redirectLogin');
		//Change the default error message displayed when someone does not successfully log in.
		$this->Auth->loginError = "Tài khoản hoặc mật khẩu chưa đúng. Xin mời bạn hãy nhập lại!";
	
	}
	
	function redirectLogin()
	{
		$user = $this->Auth->user();
		if($user['User']['group_id'] == 1)
		{
			$this->redirect (array('controller' => 'users', 'action' => 'index'));
		}
		else if($user['User']['group_id'] == 2)
		{
			$this->redirect (array('controller' => 'frontpages', 'action' => 'index'));
		}		
	}
	
	function login()
	{		
		$this->layout = 'login';
		if(is_array ($this->Auth->user ()))
		{
			$this->redirect (array('controller' => 'users', 'action' => 'index'));
		}
		//$user = $this->Auth->user();
	}
	
	function logout()
	{
		$this->Session->del ('Permissions');
		$this->redirect ($this->Auth->logout ());
	}
	
	function index()
	{
		//$this->set('users', $this->Auth->user());
		$this->User->recursive = 0;
		$this->set ('user', $this->paginate ());
	}
	
	function view($id = null)
	{
		if(! $id)
		{
			$this->Session->setFlash (__ ('Invalid User.', true));
			$this->redirect (array('action' => 'index'));
		}
		$this->set ('user', $this->User->read (null, $id));
	}
	
	function add()
	{
		if(! empty ($this->data))
		{
			$this->User->create ();
			if($this->User->save ($this->data))
			{
				$this->Session->setFlash (__ ('The User has been saved', true));
				$this->redirect (array('action' => 'index'));
			}else
			{
				$this->Session->setFlash (__ ('The User could not be saved. Please, try again.', true));
			}
		}
		$groups = $this->User->Group->find ('list');
		$this->set (compact ('groups'));
	}
	
	function edit($id = null)
	{
		if(! $id && empty ($this->data))
		{
			$this->Session->setFlash (__ ('Invalid User', true));
			$this->redirect (array('action' => 'index'));
		}
		if(! empty ($this->data))
		{
			if($this->User->save ($this->data))
			{
				$this->Session->setFlash (__ ('The User has been saved', true));
				$this->redirect (array('action' => 'index'));
			}else
			{
				$this->Session->setFlash (__ ('The User could not be saved. Please, try again.', true));
			}
		}
		if(empty ($this->data))
		{
			$this->data = $this->User->read (null, $id);
		}
		$groups = $this->User->Group->find ('list');
		$this->set (compact ('groups'));
	}
	
	function delete($id = null)
	{
		if(! $id)
		{
			$this->Session->setFlash (__ ('Invalid id for User', true));
			$this->redirect (array('action' => 'index'));
		}
		if($this->User->del ($id))
		{
			$this->Session->setFlash (__ ('User deleted', true));
			$this->redirect (array('action' => 'index'));
		}
	}
	
	function change_password()
		{
			$user_log = $this->Auth->user();
			$id = $user_log['User']['id'];
			$user = $this->User->find('first', array('conditions' => array('User.id' => $id)));
			$this->set('user', $user);
			//die(md5($this->data['User']['old_password']). '= '.$user['User']['password']);
			if ($this->data['User']['old_password'] != '')
			{
				
				if ($this->Auth->password($this->data['User']['old_password']) == $user['User']['password'])
				{
					
					if ($this->data['User']['old_password'] == $this->data['User']['new_password'])
					{
						$this->Session->setFlash('Mật khẩu mới không được trùng mật khẩu hiện tại');
					}
					elseif ($this->data['User']['new_password'] == ''){
						$this->Session->setFlash('Mật khẩu mới không được để trống');
					}
					else {
						if ($this->data['User']['new_password'] != $this->data['User']['confirm_new_password'])
						{
							$this->Session->setFlash('Xác nhận lại mật khẩu không đúng');
						} else {
							$this->User->id= $id;
							if($this->User->saveField('password', $this->Auth->password($this->data['User']['new_password'])))
							{
								$this->Session->setFlash('Mật khẩu đã được thay đổi');
							}
						}
					}
				} else {
					$this->Session->setFlash('Mật khẩu hiện tại không đúng');
				}
			}
		}

}
?>